using System;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress10;
using MobileLoader.DataSet;

namespace MobileLoader.Controls
{
    public partial class TMNLoadsXtraUserControl : XtraUserControl, IGridView, IListRefresh
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                // _controller.SupplierSaved += delegate { RefreshList(); };
            }
        }

        #endregion

        public TMNLoadsXtraUserControl()
        {
            InitializeComponent();
            Load += TmnLoadsXtraUserControlLoad;
        }

        void TmnLoadsXtraUserControlLoad(object sender, EventArgs e)
        {
            SetupShopLookupData();
            beginDateEdit.DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            endDateEdit.DateTime = beginDateEdit.DateTime.AddMonths(1).AddDays(-1);
            DevExpressHelper.SetColumnAsCurrency(colValor);
        }

        private void SetupShopLookupData()
        {
            if(_controller == null)
                return;
            tmnLojasDataSet = _controller.GetShopsOfLoads();
            shopLookUpEdit.Properties.Columns.Clear();
            shopLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("SAP", "SAP", 85, FormatType.None, "", false, HorzAlignment.Near),
                                                               new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)
                                                           });
            shopLookUpEdit.Properties.DisplayMember = tmnLojasDataSet.Loja.NomeColumn.ColumnName;
            shopLookUpEdit.Properties.ValueMember = tmnLojasDataSet.Loja.SAPColumn.ColumnName;
            shopLookUpEdit.Properties.DataSource = tmnLojasDataSet.Loja;
            //
            shopRepositoryItemLookUpEdit.Columns.Clear();
            shopRepositoryItemLookUpEdit.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("SAP", "SAP", 85, FormatType.None, "", false, HorzAlignment.Near),
                                                               new LookUpColumnInfo("Nome", "Nome", 85, FormatType.None, "", true, HorzAlignment.Near)
                                                           });
            shopRepositoryItemLookUpEdit.DisplayMember = tmnLojasDataSet.Loja.NomeColumn.ColumnName;
            shopRepositoryItemLookUpEdit.ValueMember = tmnLojasDataSet.Loja.SAPColumn.ColumnName;
            shopRepositoryItemLookUpEdit.DataSource = tmnLojasDataSet.Loja;
        }

        private ListFilter GetFilter()
        {
            var listFilter = new ListFilter
            {
                Shop = shopLookUpEdit.EditValue == null ? -1 : Convert.ToInt32(shopLookUpEdit.EditValue),
                PeriodBegin = beginDateEdit.DateTime,
                PeriodEnd = endDateEdit.DateTime
            };
            return listFilter;
        }

        public void RefreshList()
        {
            SetupShopLookupData();
            TMNCarregamentoDataSet dataSet = _controller.GetTmnLoads(GetFilter());
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Carregamento.TableName;

            DevExpressHelper.SetColumnAsCurrency(colValor);
            //DevExpressHelper.ShowGroupSummaryAsCurrency(gridView1, new List<GridColumn> { colValor });
        }

        #region IGridView

        public GridView GridView()
        {
            return gridView1;
        }

        public string ListName
        {
            get { return "TMNLoadsXtraUserControl"; }
        }

        #endregion
    }
}
